package com.cortex.papaya.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.cortex.papaya.listener.SlidViewListener;
import com.cortex.papaya.listener.SlidViewListener.OnSlidFingerListener;
import com.cortex.papaya.listener.SlidViewListener.SlidDirection;
import com.cortex.papayasearch.R;

public class PapayaRowSearch extends EditText 
implements OnEditorActionListener,OnDragListener,OnSlidFingerListener {

	private ISlidFinger slidFinger;
	private boolean isCaughtView = false;
	private String lastSearch = "";
	
	public PapayaRowSearch(Context context) {
		super(context);
	}
	public PapayaRowSearch(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PapayaRowSearch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		init();
		initHandlers();
	}
	private void init(){

	}
	private void initHandlers(){
		setOnEditorActionListener(this);
		setOnDragListener(this);
		setOnTouchListener(new SlidViewListener(3, this, SlidDirection.RIGHT,SlidDirection.LEFT));
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
	}
	public boolean isEnteredObject(){
		return isCaughtView;
	}
	public void setOnSlidFingerListener(ISlidFinger slidFinger){
		this.slidFinger = slidFinger;
	}
	@Override
	public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
		return super.onCreateInputConnection(outAttrs);
	}
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		slidFinger(true);
		return true;
	}
	@Override
	public void startedSlid() {
		this.setFocusable(true);
		requestFocus();
	}
	@Override
	public void finishedSlid(SlidDirection slidDirection) {
		if(slidDirection == SlidDirection.RIGHT){
			slidFinger(true);
		}
		else if(slidDirection == SlidDirection.LEFT){
			slidFinger(false);
		}
	}

	public String getValue() {
		return super.getText().toString();
	}
	private void slidFinger( boolean isRightSlid){
		setHint("");
		if(slidFinger != null){
			if(isRightSlid){
				if(getValue().isEmpty()){
					setHint(R.string.writeSomethingHere);
					return;
				}
				lastSearch = getValue();
				slidFinger.toRight();
			}
			else{
				clear();
				slidFinger.toLeft();
			}

		}
		boolean isShowKeyBoard = !isRightSlid;
		showKeyboard(isShowKeyBoard);
	}
	public void clear() {
		lastSearch = "";
		this.setText("");
	}
	public interface ISlidFinger{
		void toRight();
		void toLeft();
	}
	private void showKeyboard(boolean isOpen){
		InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		// what action to perform on keyboard open/hide
		if(isOpen){
			imm.toggleSoftInputFromWindow(getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
		}
		else{
			imm.hideSoftInputFromWindow(getWindowToken(), 0);
		}
	}
	@Override
	public boolean onDrag(View v, DragEvent event) {
		View view = (View) event.getLocalState();
		CategoryView rlCategory = (CategoryView) view;
		switch (event.getAction()) {
		case DragEvent.ACTION_DRAG_STARTED:
			// do nothing
			break;
		case DragEvent.ACTION_DRAG_ENTERED:
			setEnteredView(true, view);
			break;
		case DragEvent.ACTION_DRAG_EXITED:
			setEnteredView(false, view);
			break;
		case DragEvent.ACTION_DROP:
			slidFinger(true);
			break;
		case DragEvent.ACTION_DRAG_ENDED:
			setBackgroundResource(R.drawable.row_search);
			if(!isCaughtView){
				rlCategory.setVisibility(View.VISIBLE);
			}
			isCaughtView = false;
		default:
			break;
		}
		return true;
	}
	private void setEnteredView(boolean isEntered,View view){
		isCaughtView = isEntered;
		if(view instanceof CategoryView){
			CategoryView categoryView = (CategoryView)view;
			if(isCaughtView){
				// add last text to current text
				String text = lastSearch + "+";
				setText(text +  categoryView.getCategory().getName());
				setBackgroundResource(R.drawable.row_search_enter_object);
			}
			else{
				setText(lastSearch);
				setBackgroundResource(R.drawable.row_search);
			}
		}

	}
	

}
